import 'dart:math';

import 'package:flutter/material.dart';
import 'package:voice_tx/model/User.dart';
import 'package:voice_tx/utils/request.dart';
import 'package:voice_tx/widget/user_drawe.dart';

class HomePage extends StatefulWidget {
  final List<String> _cas;

  HomePage(this._cas);

  @override
  State<StatefulWidget> createState() {
    return new _StateHomePage();
  }
}

class _StateHomePage extends State<HomePage> {
  Map _user;

  // 构造一个BottomNavigationBarItem
  BottomNavigationBarItem _buildItem(IconData iconData, String title) {
    return new BottomNavigationBarItem(
        icon: new Icon(
          iconData,
          color: Colors.green,
        ),
        title: new Text(title),
        activeIcon: new Icon(
          iconData,
          color: Colors.blue,
        )
        // backgroundColor: Colors.green
        );
  }

  int _activeIndex = 1;

  Widget _getUserDrawer() {
    if (_user != null) {
      return new UserDrawer(User.fromJson(_user));
    }
    return new UserAccountsDrawerHeader(
      accountName: new Text('loading'),
      accountEmail: new Text('email'),
    );
  }

  @override
  void initState() {
    if (_user == null) {
      int a = Random.secure().nextInt(50) + 1;
      Request.getInstance().get('/user/$a').then((resp) {
        _user = resp.data;
        setState(() {});
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: const EdgeInsets.all(8.0),
        child: new MaterialApp(
          theme: ThemeData.dark(),
          home: new Scaffold(
              appBar: new AppBar(toolbarOpacity: .5, actions: [
                new IconButton(
                  icon: new Icon(Icons.ac_unit),
                  onPressed: () {
                    print('ac_unit on pressed');
                  },
                ),
                new IconButton(
                  icon: new Icon(
                    Icons.map,
                    color: Colors.green,
                  ),
                  onPressed: () {
                    print('map on pressed');
                  },
                ),
              ]),
              bottomNavigationBar: new BottomNavigationBar(
                items: [
                  _buildItem(Icons.title, '首页'),
                  _buildItem(Icons.message, '消息'),
                  _buildItem(Icons.book, '学习'),
                  _buildItem(Icons.account_balance, '我的'),
                ],
                currentIndex: _activeIndex,
                fixedColor: Colors.amber,
                onTap: (int a) {
                  setState(() {
                    _activeIndex = a;
                  });
                },
              ),
              body: new SingleChildScrollView(
                child: new ExpansionPanelList.radio(
                    expansionCallback: (int panelIndex, bool isExpanded) {
                      print('$panelIndex $isExpanded expansionCallback');
                    },
                    initialOpenPanelValue: widget._cas[0],
                    children: widget._cas.map((String key) {
                      return new ExpansionPanelRadio(
                          value: key,
                          headerBuilder: (a, b) {
                            if (b) {
                              return new Padding(
                                padding: new EdgeInsets.all(8.0),
                                child: new Text("开启"),
                              );
                            }
                            return new Padding(
                              padding: new EdgeInsets.all(8.0),
                              child: new Text("关闭r"),
                            );
                          },
                          body: new Text(key));
                    }).toList()),
              ),
              floatingActionButton: new FloatingActionButton(
                onPressed: () {
                  Navigator.pop(context);
                  print('floatingActionButton on pressed');
                },
                child: new Icon(Icons.apps),
              ),
              drawer: _getUserDrawer()),
        ));
  }
}
